import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        // m2()
        //m3()
        m4();
        //m5();
    }

    private static void m2() {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //jdbc步骤
        //1. 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 创建链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "123456");
            if (connection != null) {
                System.out.println("连接成功");
            }
            //3. 写sql
            String sql = "select * from user1 where name like ?";

            //4. 预编译sql
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "%" + name + "%");

            //5. 执行sql
            resultSet = preparedStatement.executeQuery();

            //6. 遍历结果
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String username = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id + "----" + username + "----" + pass);
            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7. 关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }

    }

    //新增
    private static void m3() {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //jdbc步骤
        //1. 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 创建链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "123456");
            if (connection != null) {
                System.out.println("连接成功");
            }
            //3. 写sql
            String sql = "insert into user1(name,pass)values (?,?)";

            //4. 预编译sql
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "王五");
            //设置参数
            preparedStatement.setString(2, "123456");

            //5. 执行增加
            int result = preparedStatement.executeUpdate();

            //6. 遍历结果
            if (result > 0) {
                System.out.println("添加成功");

            } else {
                System.out.println("添加失败");
            }


        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7. 关闭资源
            try {

                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }

    }
    //修改
    private static void m4() {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //jdbc步骤
        //1. 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 创建链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "123456");
            if (connection != null) {
                System.out.println("连接成功");
            }
            //3. 写sql
            String sql = "update user1 set name = ?,pass = ? where id = ?";

            //4. 预编译sql
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "王小明");
            //设置参数
            preparedStatement.setString(2, "888");
            preparedStatement.setInt(3, 3);

            //5. 执行增加
            int result = preparedStatement.executeUpdate();

            //6. 遍历结果
            if (result > 0) {
                System.out.println("更新成功");

            } else {
                System.out.println("更新失败");
            }


        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7. 关闭资源
            try {

                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }

    }
    //删除
    private static void m5() {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //jdbc步骤
        //1. 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 创建链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "123456");
            if (connection != null) {
                System.out.println("连接成功");
            }
            //3. 写sql
            String sql = "delete from user1  where id = ?";

            //4. 预编译sql
            preparedStatement = connection.prepareStatement(sql);

            //设置参数
            preparedStatement.setObject(1, "5");

            //5. 执行增加
            int result = preparedStatement.executeUpdate();

            //6. 遍历结果
            if (result > 0) {
                System.out.println("删除成功");

            } else {
                System.out.println("删除失败");
            }


        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7. 关闭资源
            try {

                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }

    }

}

